<template>
  <view :id="id" class="tz-search">
    <tz-input
      ref="inputRef"
      v-bind="$attrs"
      class="tz-search-input"
      confirm-type="search"
      :class="[
        { 'tz-search-input--border': border },
        { 'tz-search-input--round': round },
      ]"
      :clearable="clearable"
      @confirm="handleConfirm"
    ></tz-input>
    <view v-if="showAction" class="tz-search-action">
      <slot name="header">
        <text class="action-text" @Tap="handleTap">{{ actionText }}</text>
      </slot>
    </view>
  </view>
</template>
<script>
import { ref } from "vue";
export default {
  name: "TzSearch",
  components: {},
  inheritAttrs: false,
  props: {
    id: { type: String, default: "" },
    clearable: { type: Boolean, default: true },
    actionText: { type: String, default: "查询" },
    showAction: { type: Boolean, default: false },
    round: { type: Boolean, default: false },
    border: { type: Boolean, default: false },
  },
  emits: ["search"],
  setup(props, { emit }) {
    const inputRef = ref(null);

    const handleTap = (event) => {
      emit("search", event);
    };
    const handleConfirm = (event) => {
      emit("search", event);
    };

    //清除input框
    const clear = () => {
      inputRef?.value?.handleClear();
    };

    return { handleTap, handleConfirm, inputRef, clear };
  },
};
</script>

<style lang="scss">
.tz-search {
  display: flex;
  align-items: center;
  line-height: 50px;
  background-color: #fff;
  padding: 6px 15px;
  font-size: 16px;
  width: 100%;
  position: relative;

  .tz-search-input {
    flex: 1;
    line-height: 32px;
    background: $background-color;
    font-size: inherit;
    border-radius: 2px;
    padding-left: 10px;
    padding-right: 5px;

    .h5-input {
      min-height: 32px;
    }
    &--border {
      z-index: 2;
      @extend %border1;
    }
    &--round {
      border-radius: 999px;
      &::before {
        border-radius: 999px;
      }
    }
  }
  .tz-search-action {
    padding-left: 8px;
    margin-left: 3px;
    padding-right: 5px;
    color: $primary-color;
    font-size: inherit;
    cursor: pointer;
    user-select: none;
    line-height: 32px;
    font-size: 17px;
    &:active {
      background-color: $hover-color;
    }

    .action-text {
      font-size: inherit;
    }
  }
}
</style>
